package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.CircleNums;
import com.sam.im.heiqun.mvp.modle.PhotoInfo;
import com.sam.im.heiqun.uis.activities.CirclealldetailActivity;
import com.yuyh.library.utils.GlideUtils;
import com.yuyh.library.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.sam.im.heiqun.utils.StringUtils;

/**
 * Created by leijiawen on 2017/6/22.
 */
public class CircleDetailAdapter extends BaseRecycleViewAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<CircleNums> circleNumses = new ArrayList<>();
    private Onlongclikcklistenler onlongclikcklistenler ;

    public void setOnlongclikcklistenler(Onlongclikcklistenler onlongclikcklistenler) {
        this.onlongclikcklistenler = onlongclikcklistenler;
    }

    public CircleDetailAdapter(Context context, List<CircleNums> circleNumses) {
        this.context = context;
        this.circleNumses = circleNumses;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view1 = inflater.inflate(R.layout.layout_news_item,null);
        holder = new ItemViewHolder(view1);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        CircleNums circleNums = circleNumses.get(position);
        long creattime = circleNums.getCreattime();
        final String feedid = circleNums.getFeedid();
        String fromuserimgurl = circleNums.getFromuserimgurl();
        String fromusername = circleNums.getFromusername();
        String imgurl = circleNums.getImgurl();
        String msg = circleNums.getMsg();
        String feedmsg = circleNums.getFeedmsg();
        ItemViewHolder holder1 = (ItemViewHolder) holder;
        Glide.with(context).load(fromuserimgurl).centerCrop().error(R.drawable.head_defaul).into(holder1.imgUsericon);
        holder1.tvName.setText(fromusername);
        if (StringUtils.isEmpty(imgurl)) {
            holder1.tvMycontent.setText(feedmsg);
            if (StringUtils.isEmpty(feedmsg)) {
                holder1.tvMycontent.setVisibility(View.GONE);
            }else {
                holder1.tvMycontent.setVisibility(View.VISIBLE);
            }

            holder1.imgContent.setVisibility(View.GONE);
        } else {
            holder1.imgContent.setVisibility(View.VISIBLE);
            holder1.tvMycontent.setVisibility(View.GONE);
            final List<PhotoInfo> photos = new ArrayList<>();
            if (imgurl.contains(",")) {
                String[] splitimg = imgurl.split(",");
                for (int e = 0; e < splitimg.length; e++) {
                    PhotoInfo photoInfo = new PhotoInfo();
                    photoInfo.url = splitimg[e];
                    photos.add(photoInfo);
                }
                GlideUtils.loadImage(context, photos.get(0).url, holder1.imgContent);
//                Glide.with(context).load(photos.get(0).url).centerCrop().into(holder1.imgContent);
            } else {
                PhotoInfo photoInfo = new PhotoInfo();
                photoInfo.url = imgurl;
                photos.add(photoInfo);
//                Glide.with(context).load(photoInfo.url).centerCrop().into(holder1.imgContent);
                GlideUtils.loadImage(context, photoInfo.url, holder1.imgContent);
            }
        }
        if (circleNums.getType()== Constant.MESSAGE_TYPR_CIRCLE_PRASIE){
            holder1.imgPrasise.setVisibility(View.VISIBLE);
        }else {
            holder1.imgPrasise.setVisibility(View.GONE);
        }
        if (circleNums.getType() == Constant.MESSAGE_TYPR_CALL_ICE){
            holder1.tvContent.setText(context.getResources().getString(R.string.ti_you));
        }else {
            holder1.tvContent.setText(msg);
        }

        holder1.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("wgdinfo", "onClick: --------------feedid="+feedid);
                Intent intent = new Intent(context, CirclealldetailActivity.class);
                intent.putExtra("feedid",feedid);
                context.startActivity(intent);
            }
        });
        holder1.relativeLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                onlongclikcklistenler.onlongclick(position);
                return true;
            }
        });
        String recentlyTime = TimeUtil.getRecentlyTime(creattime);
        holder1.tvTime.setText(recentlyTime);
          /*  case 111:
                Log.i("info","进入了"+ismore);
                CheckViewHolder holder2 = (CheckViewHolder) holder;
                if (!ismore){
                    holder2.tvcheckmore.setVisibility(View.VISIBLE);
                    holder2.tvcheckmore.setEnabled(true);
                    holder2.tvcheckmore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onclickCheckmorelistenler.onclickckeckmore();
                        }
                    });
                }else {
                    holder2.tvcheckmore.setEnabled(false);
                    holder2.tvcheckmore.setVisibility(View.GONE);
                }
                break;*/
    }
    @Override
    public int getItemCount() {
        Log.i("info","size=="+circleNumses.size());
        return circleNumses.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_usericon)
        ImageView imgUsericon;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.img_content)
        ImageView imgContent;
        @BindView(R.id.tv_mycontent)
        TextView tvMycontent;
        @BindView(R.id.relative)
        RelativeLayout relativeLayout ;
        @BindView(R.id.img_prasise)
        ImageView imgPrasise ;
        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    static class CheckViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tvcheckmore)
        TextView tvcheckmore;

        CheckViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
    public interface Onlongclikcklistenler{
        void onlongclick(int position);
    }
}
